// pages/setting/setting.ts

import{getPicTypeList,setPicTypeList,setPicScore,setWordScore} from '../../../../api/api'

Page({
  /**
   * 页面的初始数据
   */
  data: {
    picType:[],
    picList:[],
    fileList: [],
    textNumber:'',
    lexiconId:3,
    score:null,
    lexiconName:'',
    option1: [
      { text: '色情词库', value: 3 },
      { text: '暴恐词库', value: 4 },
      { text: '反对词库', value: 5 },
      { text: '贪腐词库', value: 6 },
      { text: '民生词库', value: 7 },
      { text: '测试词库1', value: 8 },
    ],
    option2: [
      { text: '色情', value: 1 },
      { text: '暴恐', value: 2 },
      { text: '恶心', value: 3 },
      { text: '广告', value: 4 },
      { text: '政治敏感', value: 5 },
    ],
    value1: 3,
    value2: 3,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    // getPicTypeList({
    // }).then(res=>{
    //   // console.log(res.data.data.picTypeList);
    //   this.setData({
    //     picType:res.data.data.picTypeList
    //   })
    // })
    this.showData()
  },
  showData(){
    getPicTypeList({
    }).then(res=>{
      console.log(res.data.data.picTypeList);
      this.setData({
        picType:res.data.data.picTypeList
      })
    })
  },
  //下拉绑定value值变化触发事件
  ChooseItems(e){
    const currentPage = getCurrentPages()[getCurrentPages().length - 1];
    let id = e.detail
    const option = currentPage.data.option1;
    const text = option.find(obj=> obj.value=== id)
    // console.log(text.text);
    // console.log(text.value);
    this.setData({
      lexiconId: text.value,
      lexiconName: text.text
    })
  },
  wordInput(value){
    const text = parseInt(value.detail.value);
    console.log(typeof(text));
    console.log("您输入敏感字扣分分数为：" + text);
    this.setData({
      score: text
    })
  },
  picInput(value){
    let text = value.detail.value;
    console.log("您输入违规图片扣分分数为：" + text);
    this.setData({
      score: text
    })
  },
  //敏感文字扣分设置提交
  wordUpdate(){
    setWordScore({
      id: this.data.lexiconId,
      score: this.data.score
    }).then(res=>{
      console.log(res);
    })
  },

  //敏感图片扣分设置提交
  picUpdate(){
    setPicScore({
      id: this.data.lexiconId,
      score: this.data.score
    }).then(res=>{
      console.log(res);
    })
  },
  //审核多选设置
  onChange(event) {
    const newArr=event.detail.value
    let oldArr=this.data.picType
    for(let i=0;i<oldArr.length;i++){
      oldArr[i].isChoice=0
      for(let j=0;j<newArr.length;j++){
        if(oldArr[i].name==newArr[j]){
          oldArr[i].isChoice=1
        }
      }
    }
    this.setData({
      picType:oldArr,
    });
  },

  //提交敏感图片修改
  checkboxChange(){
    let oldArr=this.data.picType
    // console.log('列表：',oldArr)
    setPicTypeList({
      picTypeList: oldArr
    }).then(res=>{
      console.log(res);
    })
  },

  //选择图片
  chooseImage(){
    wx.chooseMessageFile({
      count: 10,
      type: 'image',
      success: (res) => {
        wx.showToast({
          title: '上传中',
          icon: 'loading',
          duration: 2000,
          mask: "true"
        })
        const { tempFiles } = res;
        const pic = tempFiles[0];
        const { picList } = this.data;
        picList.push(pic);
        this.setData({
          picList: picList,
        });
      },
    });
  },

  //提交机要图片
  submitPic(){
    wx.showLoading({
      title: '加载中',
      mask: "true"
    })
    this.setData({
      loading: true,
    });
    const { picList } = this.data;
    const picPath = picList[0].path;
    wx.uploadFile({
      // url: 'http://110.40.174.156:8998/auditSet/uploadCdPic', // 替换为你的审核接口地址
      url: 'http://127.0.0.1:8998/auditSet/uploadCdPic', // 替换为你的审核接口地址
      filePath: picPath,
      name: 'file',
      success: (res) => {
        wx.hideLoading();
        // console.log(res.data);
        const data = JSON.parse(res.data);
        wx.showToast({
          title: data.data,
          icon: 'success',
          duration: 1000
        })
        if (data) {
          this.setData({
            loading: false,
          });
        }
      },
      fail: (err) => {
        console.log("上传文件类型不符合要求");
      },
    });
  },

  //绝密文本文件添加
  secretoryChoose(){
    wx.chooseMessageFile({
      count: 1,
      type: 'file',
      success: (res) => {
        wx.showToast({
          title: '上传中',
          icon: 'loading',
          duration: 1500,
          mask: "true"
        })
        const { tempFiles } = res;
        const file = tempFiles[0];
        const { fileList } = this.data;
        fileList.push(file);
        this.setData({
          fileList: fileList,
        });
      },
    });

  },
  //文本输入双向绑定
  textInput(value){
    let text = value.detail.value;
    console.log("您输入的内容为：" + text);
    this.setData({
      textNumber: text
    })
  },

  //提交绝密文本文件
  filesSubmmits(){
    wx.showLoading({
      title: '加载中',
      mask: "true"
    })
    this.setData({
      loading: true,
    });
    const { fileList } = this.data;
    const filePath = fileList[0].path;
    wx.uploadFile({
      // url: 'http://110.40.174.156:8998/auditSet/uploadCdFile', // 替换为你的审核接口地址
      url: 'http://127.0.0.1:8998/auditSet/uploadCdFile', // 替换为你的审核接口地址
      filePath: filePath,
      name: 'file',
      formData:{
        number: this.data.textNumber
      },
      success: (res) => {
        wx.hideLoading();
        console.log(res.data);
        const data = JSON.parse(res.data);
        if (data) {
          wx.showToast({
            title: '上传成功',
          })
          this.setData({
            loading: false,
            textNumber:''
          });
          this.showData()
        }
      },
      fail: (err) => {
        console.log("上传文件类型不符合要求");
      },
    });
  },
  WordModel(){

  },
  DocumentModel(){
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})